package com.beteng.su.betengsu.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.beteng.su.betengsu.R;
import com.beteng.su.betengsu.base.BaseActivity;
import com.beteng.su.betengsu.ui.fragment.SettingFragment;
import com.beteng.su.betengsu.ui.fragment.task.TaskMainFragment;
import com.zhy.autolayout.AutoRelativeLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.droidlover.xdroid.base.XFragment;
import me.majiajie.pagerbottomtabstrip.NavigationController;
import me.majiajie.pagerbottomtabstrip.PageNavigationView;

/**
 * Created by chenzhimin on 2017/9/5.
 */

public class MainActivity extends BaseActivity {


    NavigationController mNavigationController;
    @BindView(R.id.viewPager)
    ViewPager mViewPager;
    @BindView(R.id.tab)
    PageNavigationView mTab;
    @BindView(R.id.toolbar_back)
    AutoRelativeLayout mToolbarBack;
    @BindView(R.id.toolbar_title)
    TextView mToolbarTitle;



    private List<XFragment> mFragments = new ArrayList<>();

    public static void start(Context context) {
        Intent starter = new Intent(context, MainActivity.class);
        context.startActivity(starter);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

        mToolbarTitle.setText("任务");

        mToolbarBack.setVisibility(View.GONE);

        mFragments.add(TaskMainFragment.newInstance());
        mFragments.add(SettingFragment.newInstance());

        mNavigationController = mTab.material()
                .addItem(android.R.drawable.ic_menu_upload_you_tube, "任务")
                .addItem(android.R.drawable.ic_menu_info_details, "设置")
                .setMessageNumberColor(getResources().getColor(R.color.colorPrimary))
                .build();

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewPager);
        viewPager.setAdapter(new TestViewPagerAdapter(getSupportFragmentManager()));

        //自动适配ViewPager页面切换
        mNavigationController.setupWithViewPager(viewPager);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                switch (position) {
                    case 0:
                        mToolbarTitle.setText("任务");
                        break;
                    case 1:
                        mToolbarTitle.setText("设置");
                        break;
                    default:
                        mToolbarTitle.setText("任务");
                }

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_main;
    }



    //下面几个类都是为了测试写的
    private class TestViewPagerAdapter extends FragmentPagerAdapter {

        public TestViewPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragments.get(position);
        }

        @Override
        public int getCount() {
            return mNavigationController.getItemCount();
        }
    }

}
